import { defineStore } from 'pinia'
import { useLoginApi } from '@/api/login/index.js'
import { useMenuApi } from '@/api/menu'

const loginApi = useLoginApi()
const menuApi = useMenuApi()

/**
 * 用户信息
 */
export const useUserStore = defineStore('user', {
  state: () => ({
    user: {},
    menus: []
  }),
  actions: {
    // 获取用户信息
    async getInfo() {
      const res = await loginApi.getInfo()
      this.user = res
    },
    // 获取导航菜单
    async getMenuNav() {
      const res = await menuApi.getMenuNav()
      this.menus = res
    },
    // 登录
    async toLogin(data) {
      const res = await loginApi.login(data)
      sessionStorage.setItem('token', res.token)
    },
    // 登出
    async toLogout() {
      await loginApi.logout()
      this.user = {}
      this.menus = []
      sessionStorage.removeItem('token')
    }
  }
})
